package com.mohan.dlxj.service.impl;

import com.mohan.dlxj.common.Constants;
import com.mohan.dlxj.common.JsonResult;
import com.mohan.dlxj.common.R;
import com.mohan.dlxj.mapper.UserMapper;
import com.mohan.dlxj.pojo.Users;
import com.mohan.dlxj.service.UserService;
import com.mohan.dlxj.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: admin
 * Description: 处理业务逻辑
 * Version: V1.0
 */
@Service
public class UserServiceImpl implements UserService {//alt+enter
    //调用Mapper
    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public R login(String login_account, String user_pwd) {
        R r = new R();
        //②客户端密码加密->密文
        String new_user_pwd = MD5Utils.encrypt(user_pwd, MD5Utils.MD5_KEY);
        //③将登录账号与加密后的密码传递给Mapper(mybatis)进行数据访问
        Users user = userMapper.login(login_account,new_user_pwd);//验证后登录成功，返回一条数据，否则为null
        if(user != null){//登录成功
            r.setCode(200);
            r.setMsg("登录成功！");
            r.setData(user);
        }else{//登录失败
            r.setCode(500);
            r.setMsg("账号或密码有误！");
        }
        return r;
    }

    @Override
    public R checkPassword(String login_account, String user_pwd) {
        //②客户端密码加密->密文
        String new_user_pwd = MD5Utils.encrypt(user_pwd, MD5Utils.MD5_KEY);
        //③将登录账号与加密后的密码传递给Mapper(mybatis)进行数据访问
        Users user = userMapper.login(login_account,new_user_pwd);//验证后登录成功，返回一条数据，否则为null
        R r = new R();
        if(user == null){//证明不是当前登录账号的密码
            r.setCode(500);
            r.setMsg("当前密码与登录账户密码不符");
        }
        return r;
    }

    @Override
    public R updatePassword(String login_account, String newpassword) {
        //将新密码加密
        String user_pwd = MD5Utils.encrypt(newpassword, MD5Utils.MD5_KEY);
        int result = userMapper.updatePassword(login_account,user_pwd);
        R r = new R();
        if(result > 0){//修改成功
            r.setCode(200);
            r.setMsg("操作成功！");
        }
        return r;
    }

    @Override
    public JsonResult findUsers(Integer page, Integer limit) {
         JsonResult jsonResult = new JsonResult();
         jsonResult.setCode(0);
         jsonResult.setMsg("");
         //第一个参数(偏移量)
         Integer currentPage = (page-1)*limit;
         //第二个参数
         Integer pageSize = limit;
         List<Users> usersList = userMapper.findUsers(currentPage,pageSize); //调用Mappper查询出来 10
         jsonResult.setCount(userMapper.totalSize());//总条数
         jsonResult.setData(usersList);
         return jsonResult;
    }

    @Override
    public R batchUserdel(Integer[] ids) {
        R r = new R();
        try{
            //删除：真删除 、假删除
            userMapper.batchUserdel(ids);
            //程序走到这里代表删除成功了
            r.setCode(200);
            r.setMsg("操作成功");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员");
        }
        return r;
    }

    @Override
    public R addUser(Users users) {
        R r = new R();
        try{
            String user_pwd = MD5Utils.encrypt(Constants.INIT_PASSWORD,MD5Utils.MD5_KEY);
            users.setUser_pwd(user_pwd);
            int result = userMapper.addUser(users);
            if(result > 0){//操作成功
                //程序走到这里代表删除成功了
                r.setCode(200);
                r.setMsg("操作成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员");
        }
        return r;
    }
}
